package service

import (
	"boy-go/modules/system/model"
	"boy-go/modules/system/repository"
	"boy-go/modules/system/vo"
	"boy-go/pkg/request"
	"github.com/jinzhu/copier"
)

func NewSysNoticeService() SysNoticeService {
	return &sysNoticeService{
		SysNoticeRepo: repository.NewSysNoticeRepository(),
	}
}

type sysNoticeService struct {
	SysNoticeRepo *repository.SysNoticeRepository
}

// GetById 根据id获取配置信息
func (s *sysNoticeService) GetById(noticeId int64) (*vo.SysNoticeModel, error) {
	m, err := s.SysNoticeRepo.GetById(noticeId)
	if err != nil {
		return nil, err
	}
	rsp := new(vo.SysNoticeModel)
	if err := copier.Copy(rsp, m); err != nil {
		return nil, err
	}
	return rsp, nil
}

// List 获取通知公告表列表
func (s *sysNoticeService) List(req *vo.QuerySysNoticeReq) (*vo.ListSysNoticeRsp, error) {

	var pageReq = &request.PageRequest{
		PageNum:  req.PageNum,
		PageSize: req.PageSize,
	}

	list, total, err := s.SysNoticeRepo.List(req, pageReq)

	if err != nil {
		return nil, err
	}

	itemList := make([]*vo.SysNoticeModel, 0)
	for _, v := range list {
		item := new(vo.SysNoticeModel)
		if err := copier.Copy(item, &v); err != nil {
			continue
		}
		itemList = append(itemList, item)
	}
	return &vo.ListSysNoticeRsp{
		List:  itemList,
		Total: total,
	}, nil
}

// Add 添加通知公告表
func (s *sysNoticeService) Add(req *vo.AddSysNoticeReq) (*vo.AddSysNoticeRsp, error) {
	m := new(model.SysNotice)
	err := copier.Copy(m, req)
	if err != nil {
		return nil, err
	}
	if err := s.SysNoticeRepo.Save(m); err != nil {
		return nil, err
	}
	return &vo.AddSysNoticeRsp{
		NoticeId: m.NoticeId,
	}, nil
}

// Edit 修改通知公告表
func (s *sysNoticeService) Edit(req *vo.EditSysNoticeReq) (*vo.EditSysNoticeRsp, error) {
	m := new(model.SysNotice)
	err := copier.Copy(m, req)
	if err != nil {
		return nil, err
	}
	if err := s.SysNoticeRepo.Update(m); err != nil {
		return nil, err
	}
	return &vo.EditSysNoticeRsp{
		NoticeId: m.NoticeId,
	}, nil
}

// Del 删除通知公告表
func (s *sysNoticeService) Del(noticeId int64) error {
	return s.SysNoticeRepo.Delete(noticeId)
}

func (s *sysNoticeService) BatchDel(ids []string) error {
	return s.SysNoticeRepo.BatchDelete(ids)
}
